﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using HeritageAcademy.Models.DataModels;
using HeritageAcademy.Models.Loaders;
using HeritageAcademy.Shared;
using System.ComponentModel;
using System.Windows;
using HeritageAcademy.Window.Resources;

namespace HeritageAcademy.Models.ViewModels
{
    public class ResultsViewModel : INotifyPropertyChanged
    {
        #region Parameters

        string _testId = "1";       
        TestDataLoader _testDataLoader; // load test data
        TestResultsLoader _testResultsLoader; // load test results
        Dictionary<string, string> _enigma; // right answers dict
        
        private ObservableCollection<ResultViewModel> _results;
        private int _ra, _wa;

        #endregion

        #region Ctor

        public ResultsViewModel()
        {
            _results = new ObservableCollection<ResultViewModel>();
        }

        #endregion

        #region Properties

        public ObservableCollection<ResultViewModel> Results
        {
            get { return _results ?? (_results = new ObservableCollection<ResultViewModel>()); }
            set 
            { 
                _results = value;
                OnPropertyChanged("Results");
            }
        }

        public int NumberOfRightAnswers
        {
            get { return _ra; }
            set 
            { 
                _ra = value;
                OnPropertyChanged("NumberOfRightAnswers");
            }
        }

        public int NumberOfWrongAnswers
        {
            get { return _wa; }
            set 
            { 
                _wa = value;
                OnPropertyChanged("NumberOfWrongAnswers");
            }
        }
        
        public string TestId
        {
            get { return _testId; }
            set { _testId = value; }
        }

        #endregion

        #region Public Methods

        public void InitializeResults()
        {
            _testDataLoader = new TestDataLoader(new TestWizard(), IOHelper.ApplicationExecutablePath + Strings.Tests_Path);
            _enigma = new Dictionary<string, string>();

            _ra = 0;
            _wa = 0;
            
            if (_testDataLoader.Model.Id == _testId)
                foreach (var page in _testDataLoader.Model.TestPage)
                    _enigma.Add(page.Id, page.Code);
        }

        public void GetTestResults()
        {
            _testResultsLoader = new TestResultsLoader(new TestResults());
            bool pageStatus;

            _results.Clear();
            if (_testResultsLoader.Model.TestId == _testId)
            {
                foreach (var page in _testResultsLoader.Model.Results)
                {
                    pageStatus = true;
                    foreach (var answer in page.Answers)
                    {
                        if (pageStatus)
                        {
                            if (answer.Status == "false")
                            {
                                foreach (var ans in DataFileParsing.SeparateString(_enigma[page.Id], Strings.Results_Separator))
                                    if (answer.Id == ans) pageStatus = false;
                            }
                            else
                            {
                                pageStatus = false;
                                foreach (var ans in DataFileParsing.SeparateString(_enigma[page.Id], Strings.Results_Separator))
                                    if (answer.Id == ans) pageStatus = true;
                            }
                        }
                    }

                    ResultViewModel data = new ResultViewModel();
                    data.PageNumber = page.Id;
                    data.Time = page.Time;
                    if (pageStatus)
                    {
                        data.Status = Strings.Results_Status_True;
                        ++_ra;
                    }
                    else
                    {
                        data.Status = Strings.Results_Status_False;
                        ++_wa;
                    }
                    _results.Add(data);
                }
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }

    public class ResultViewModel
    {
        public string PageNumber { get; set; }
        public string Time { get; set; }
        public string Status { get; set; }
    }
}
